package com.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

//代码自动生成
public class KuangCode {

    public static void main(String[] args) {
        // 需要构建一个 代码自动生成器对象
        AutoGenerator generator = new AutoGenerator();
        //配置策略

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("YunDian");
        gc.setFileOverride(true);//是否覆盖
        gc.setServiceName("%sService");// 去掉Service的I前缀
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);//设置日期类型
        gc.setSwagger2(false);//设置Swagger文档
        generator.setGlobalConfig(gc);
        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/vueblog?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        generator.setDataSource(dsc);
        //3、包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("markerhub");
        pc.setController("controller");
        pc.setParent("com");//设置父目录 模块会生成到父目录下
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        generator.setPackageInfo(pc);
        //4、策略配置
        StrategyConfig strateg = new StrategyConfig();
        String[] tables = {"m_user","m_blog"};
        strateg.setInclude(tables); //设置要映射的表名
        strateg.setNaming(NamingStrategy.underline_to_camel);
        strateg.setColumnNaming(NamingStrategy.underline_to_camel);
        strateg.setEntityLombokModel(true);//自动添加Lombok
        strateg.setTablePrefix("m_");
        //自动填充配置
        TableFill gmtCreate = new TableFill("created", FieldFill.INSERT);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        strateg.setTableFillList(tableFills);
        //乐观锁
        strateg.setRestControllerStyle(true);
        strateg.setControllerMappingHyphenStyle(true);//连字符风格url localhost:8080//helle_id_2
        generator.setStrategy(strateg);


        generator.execute();
    }
}
